package com.woniuxy.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.common.Result;
import com.woniuxy.entity.OperationLog;
import com.woniuxy.service.OperationLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/log")
public class OperationLogController {
    @Autowired
    private OperationLogService operationLogService;

    //查询所有
//    @RequestMapping("/findPage")
//    public PageBean<OperationLog> findPage(Integer page, Integer size){
//        //参数不需要自己获取，只要提交的参数名字相同，就自动传入到方法中
//        System.out.println("第" + page + "页，每页大小：" + size);
//        PageBean<OperationLog> pageBean = operationLogService.findPage(page, size);
//        return pageBean;
//    }
    //查询所有
  //  @RequestMapping("/findByConditionPage")
//    public PageBean<OperationLog> findByCondition(@RequestBody Map<String, Object> condition){
//        int size = (int) condition.get("size");
//        int page = (int) condition.get("page");
//        //参数不需要自己获取，只要提交的参数名字相同，就自动传入到方法中
//        System.out.println("第" + page + "页，每页大小：" + size);
//        PageBean<OperationLog> pageBean = operationLogService.findByCondition(condition,page, size);
//        return pageBean;
//    }

    //分页显示
    @PostMapping("/findPage")
    public ResponseEntity<PageInfo<OperationLog>> findPage(@RequestBody Map<String,Object> condition){
        int pageNum = (int) condition.get("pageNum");
        int pageSize = (int) condition.get("pageSize");
        PageInfo<OperationLog> pageInfo = operationLogService.findPage(condition, pageNum, pageSize);
        //返回结果
        return ResponseEntity.ok(pageInfo);
    }
    /**
     * 添加日志
     * @RequestBody 将JSON字符串转成员工对象，封装属性值
     */
    @RequestMapping("/add")
    public int add(@RequestBody OperationLog operationLog) {
        System.out.println(operationLog);
        return operationLogService.addOperationLog(operationLog);
    }
    /**
     * 查找日志
     */
    public OperationLog findByName(String methodName){
        return operationLogService.findByName(methodName);
    }
    /**
     * 批量删除
     */
    @PostMapping("/deleteBatch")
    public Result<Integer> deleteBatch(@RequestBody String[] ids) {
        int row = operationLogService.deleteBatch(ids);
        return Result.success("批量删除日志记录成功", row);
    }
}
